/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.spms.source.model;

import com.wisnu.spms.service.dao.DbEmployeeDao;
import com.wisnu.spms.service.util.SpringUtil;
import com.wisnu.spms.source.Employee;
import com.wisnu.spms.source.listener.PanelLoginListener;

/**
 *
 * @author Wisnu Wardoyo
 */
public class PanelLoginModel {

    private String username;
    private String password;

    private PanelLoginListener listener;

    public PanelLoginModel(PanelLoginListener listener) {
        this.listener = listener;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
        onChange(this);
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
        onChange(this);
    }

    protected void onChange(PanelLoginModel model) {
        listener.onChange(model);
    }

    public boolean isLogin() throws Exception {
        DbEmployeeDao dao = SpringUtil.getDbEmployeeDao();
        Employee.setEmployee(dao.getEmployee(username, password));
        return Employee.getEmployee() != null;
    }

    public void updateEmployee() throws Exception {
        DbEmployeeDao dao = SpringUtil.getDbEmployeeDao();
        Employee.getEmployee().setEmployeeIsLogin("Yes");
        dao.updateEmployee(Employee.getEmployee());
    }

}
